package org.vectrics.recruiter.struts.admin;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Category;
import org.apache.struts.action.ActionForm;
import org.vectrics.common.util.StringUtils;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.location.Location;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.UserPrivilegeType;

public class SaveLocationAction extends VectricsStrutsAction {
    private static Category log = Category.getInstance(SaveLocationAction.class);

    public static String CURRENT_JOB_KEY = "current-job";

    public static String ID_PARAM = "id";

    public static String NAME_PARAM = "name";

    public static String DESCRIPTION_PARAM = "description";

    public SaveLocationAction() {
    }

    @Override
    public String doExecute(final VectricsActionData actionData) throws JspException {
        if (actionData.wasButtonPressed("cancel") == false) {
            ActionForm form = actionData.getForm();

            LocationForm locationForm = (LocationForm) form;

            Location location = null;
            Location parent = null;
            if (StringUtils.isNullOrBlank(locationForm.getId())) {
                log.debug("Location is new, find parent");
                if (StringUtils.isNullOrBlank(locationForm.getParentId())) {
                    locationForm.setTypeCode("country");
                } else {
                    parent = CoreServiceLocator.getLocationService().findLocation(new Long(locationForm.getParentId()));
                }
                location = CoreServiceLocator.getLocationService().createLocation(ThreadProperties.getDomain(), parent,
                        locationForm.getName(), locationForm.getDescription(), locationForm.getTypeCode(),
                        locationForm.getAbbreviation());
            } else {
                location = CoreServiceLocator.getLocationService().findLocation(new Long(locationForm.getId()));
                if (locationForm.getParentId() != null && !locationForm.getParentId().trim().equals("")) {
                    parent = CoreServiceLocator.getLocationService().findLocation(new Long(locationForm.getParentId()));
                }
                location.setDescription(locationForm.getDescription());
                location.setAbbreviation(locationForm.getAbbreviation());
                location.setName(locationForm.getName());
                if (parent != null) {
                    location.setParent(parent);
                }
                location.setTypeCode(locationForm.getTypeCode());

                boolean approved = false;
                UserPrivilegeType approvedRole = CoreServiceLocator.getPersonService().findUserPrivilegeType(
                        "location.approve");
                if (ThreadProperties.getPerson().hasDomainOrGlobalPrivilege(approvedRole, ThreadProperties.getDomain())) {
                    approved = true;
                }
                location.setApproved(approved);

                CoreServiceLocator.getLocationService().updateLocation(location);
            }
            ThreadProperties.forcedCommit();
        }
        return "success";
    }

}
